import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

@Slf4j
public class SortKeys {

    @Test
    public void sort() throws IOException {
        // /home/liangqq/Desktop/lqq/redis
        int total = 0;

        Map<String, Integer> keyTabs = new HashMap<>();

        File file = new File("/home/liangqq/Desktop/lqq/redis/no_ttl.log");
        FileReader fileReader = new FileReader(file);
        BufferedReader br = new BufferedReader(fileReader);

        while (true) {
            String s = br.readLine();
            if (s == null){
                break;
            }

            int index = s.lastIndexOf(":");
            index = index <= 0 ? s.length() : index;
            //log.info(s);
            //log.info(String.valueOf(index));
            String tempStr = s.substring(0, index);
            Integer count = keyTabs.get(tempStr);
            if(count == null){
                keyTabs.put(tempStr, 0);
            }else {
                keyTabs.put(tempStr, count + 1);
            }

        }


        List<Map.Entry<String, Integer>> list = new LinkedList<>(keyTabs.entrySet());

        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){
            @Override
            public int compare(Map.Entry<String, Integer> t1, Map.Entry<String, Integer> t2) {
                if(t1.getValue() > t2.getValue()){
                    return -1;
                }else if(t1.getValue() < t2.getValue()){
                    return 1;
                }
                return 0;
            }
        });


        for (Map.Entry<String, Integer> entry : list){
            System.out.println(entry.getValue() + "=====================" + entry.getKey());
        }


    }

}
